package com.tl.sun.ui.dialog;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.tl.sun.AppContext;
import com.tl.sun.OkHeader;
import com.tl.sun.R;
import com.tl.sun.helper.UIHelper;
import com.tl.sun.manager.MDConstant;
import com.tl.sun.model.BaseModel;
import com.tl.sun.model.ExceedUserEnetity;
import com.tl.sun.ui.activity.LoginActivity;
import com.tl.sun.ui.adapter.CoilAdapter;
import com.tl.sun.utils.ToastUtil;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * 作者：HQY on 16/11/2 13:48
 * 邮箱：hqy_xz@126.com
 */

public class HintDialog {


    private AlertDialog outDialog;

    private TextView outCancel;
    private RecyclerView rvHint;

    private CoilAdapter coilAdapter;
    private List<ExceedUserEnetity> list = new ArrayList<>();
    private Activity activitys;
    private String type;
    private String mCloseType;

    public HintDialog(final Activity activity, String dialogType, List<ExceedUserEnetity> content, int flag, final String closeType) {

        outDialog = new AlertDialog.Builder(activity, R.style.loading_dialog).create();
        outDialog.show();
        //关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
        Window window = outDialog.getWindow();
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.gravity = Gravity.CENTER;
        wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
        wlp.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(wlp);
        window.setContentView(R.layout.dialog_hint);
        outCancel = (TextView) window.findViewById(R.id.tv_out_cancel);
        list = content;
        activitys = activity;
        type = dialogType;
        mCloseType = closeType;
        if (flag == MDConstant.DIALOG_FLAG_LOGIN_CONFLICT) {
            outDialog.setCanceledOnTouchOutside(false);
            outDialog.setCancelable(false);
        } else {
            outDialog.setCanceledOnTouchOutside(true);
            outDialog.setCancelable(true);
        }
        rvHint = (RecyclerView) window.findViewById(R.id.rv_hint_ip);
        outCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (closeType.equals("1")) {
                    Intent splash = new Intent(activity, LoginActivity.class);
                    activity.startActivity(splash);
                    activity.finish();

                }
                close();
            }
        });
        init();
    }

    public void init() {
        coilAdapter = new CoilAdapter(list, "1");
        rvHint.setLayoutManager(new LinearLayoutManager(activitys));
        rvHint.setAdapter(coilAdapter);
        rvHint.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {

            }

            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                super.onItemChildClick(adapter, view, position);
                ToastUtil.showToast("下线成功");
                clearSession(list.get(position));
            }
        });
    }

    public void close() {
        outDialog.dismiss();
    }


    private void clearSession(ExceedUserEnetity userInfo) {
        HashMap<String, String> map = new HashMap<>();
        map.put("session_id", userInfo.getSession_id());
        map.put("username", userInfo.getUsername());
        JSONObject jsonObject = new JSONObject(map);

        OkGo.getInstance().addCommonHeaders(OkHeader.getCommonHead(activitys)).post(AppContext.getInstance().getBaseUrl() + "client/clear_session")
                .tag(activitys)
                .upJson(jsonObject.toString())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Gson gson = new Gson();
                        BaseModel baseModel = gson.fromJson(s, BaseModel.class);
                        if (baseModel.getRet().equals("0")) {
                            if (type.equals(UIHelper.SPLASH_DIALOG) || mCloseType.equals("1")) {
                                Intent splash = new Intent(activitys, LoginActivity.class);
                                activitys.startActivity(splash);
                                activitys.finish();
                            }
                            close();
                        } else {
                            ToastUtil.showToast(baseModel.getMsg());
                        }
                    }
                });

    }

}
